﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace WebUtility
{
    public class ColumnDescriptor : ICollection<Column>
    {
        #region Variables
        protected List<Column> _columns;
        protected bool _isReadOnly;
        #endregion
        
        

        #region constructor
        public ColumnDescriptor()
        {
            _columns = new List<Column>();
        }
        #endregion
        

        #region Implement method
        public void Add(Column item)
        {
            _columns.Add(item);
        }

        public void Clear()
        {
            _columns.Clear();
        }

        public bool Contains(Column item)
        {
            return _columns.Contains(item);
        }

        public void CopyTo(Column[] array, int arrayIndex)
        {
            _columns.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _columns.Count; }
        }

        public virtual bool IsReadOnly
        {
            get { return _isReadOnly; }
        }

        public bool Remove(Column item)
        {
            return _columns.Remove(item);
        }

        public IEnumerator<Column> GetEnumerator()
        {
            return _columns.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _columns.GetEnumerator();
        }
        #endregion


        #region Public Methods
        public virtual void AddActionColumns(params ColumnType[] colTypes)
        {

        }
        
        /// <summary>
        /// Dựa vào kiểu dữ liệu của data tạo các cột
        /// </summary>
        /// <param name="t">type</param>
        public virtual void CreateColumnByType(Type t)
        {
            t.GetProperties().ToList().ForEach(p=>
                {
                    Column col = new Column();
                    col.PropertyName = p.Name;
                    col.HeaderText = p.Name;
                    Type propType = p.PropertyType;
                    if(propType == typeof(System.Int32) )
                    {
                        col.ColType = ColumnType.INT;
                    }else if(propType == typeof(DateTime))
                    {
                        col.ColType = ColumnType.DATETIME;
                    }
                    this.Add(col);
                });
        }
        #endregion


        #region Properties
        public HtmlHelper Helper { get; set; }
        
        #endregion
    }
}
